using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using InfoExpress.HIV.WinApp.Utils;
using InfoExpress.HIV.WinApp.Popups;
using InfoExpress.WinApp.Utils;
using System.Data;
using InfoExpress.HIV.Domains;

namespace InfoExpress.HIV.WinApp.Forms
{
  public partial class FrmDsDieuTriArv : XtraForm
  {
    private DataTable _dtbDsBenhNhan = null;
    private TList<UtDmdv> _donViList;

    public FrmDsDieuTriArv()
    {
      InitializeComponent();
    }

    #region Event Handlers

    private void FrmDsDieuTriArv_Load(object sender, EventArgs e)
    {
      BindData();
      LoadData();
    }

    #region Command Handlers

    private void btnSearch_Click(object sender, EventArgs e)
    {
      pceSearch.ClosePopup();
      LoadData();
    }

    private void btnClear_Click(object sender, EventArgs e)
    {
      UIUtils.ClearTextEdit(txtHoTen);
      UIUtils.ClearTextEdit(txtMaThe);
      UIUtils.ClearOtherEdit(deTuNgay);
      UIUtils.ClearOtherEdit(deDenNgay);
      UIUtils.ClearOtherEdit(lueDonVi);
    }

    private void btnView_Click(object sender, EventArgs e)
    {
      if (gvDsBenhNhan.FocusedRowHandle >= 0)
      {
        int benhNhanId = (int)gvDsBenhNhan.GetFocusedDataRow()["BenhNhanId"];
        using (FrmDsPhacDo frm = new FrmDsPhacDo(benhNhanId) { Owner = this })
        {
          if (frm.ShowDialog() == DialogResult.OK)
          {
            MainFormUtils.SetDefaultStatus();
          }
        }
      }
    }

    private void btnEdit_Click(object sender, EventArgs e)
    {
      if (gvDsBenhNhan.FocusedRowHandle >= 0)
      {
        DataRow dataRow = gvDsBenhNhan.GetFocusedDataRow();
        int benhNhanId = (int)dataRow["BenhNhanId"];
        string maBenhNhan = (string)dataRow["MaBenhNhan"];
        using (FrmThayDoiPhacDo frm = new FrmThayDoiPhacDo(benhNhanId, maBenhNhan) { Owner = this })
        {
          if (frm.ShowDialog() == DialogResult.OK)
          {
            MainFormUtils.SetDefaultStatus();
          }
        }
      }
    }

    private void btnReject_Click(object sender, EventArgs e)
    {
      if (gvDsBenhNhan.FocusedRowHandle >= 0)
      {
        DataRow dataRow = gvDsBenhNhan.GetFocusedDataRow();
        int benhNhanId = (int)dataRow["BenhNhanId"];
        string maBenhNhan = (string)dataRow["MaBenhNhan"];

        using (FrmChamDutDieuTri frm = new FrmChamDutDieuTri(benhNhanId, maBenhNhan) { Owner = this })
        {
          if (frm.ShowDialog() == DialogResult.OK)
          {
            gvDsBenhNhan.DeleteRow(gvDsBenhNhan.FocusedRowHandle);
            MainFormUtils.SetDefaultStatus();
          }
        }
      }
    }

    private void btnPrint_Click(object sender, EventArgs e)
    {

    }

    #endregion

    #region Data Loading

    private void bwDataLoading_DoWork(object sender, DoWorkEventArgs e)
    {
      DateTime? tuNgay = (deTuNgay.EditValue != null) ?
              (DateTime?)deTuNgay.EditValue : null;
      DateTime? denNgay = (deDenNgay.EditValue != null) ?
        (DateTime?)deDenNgay.EditValue : null;
      string donVi = (lueDonVi.EditValue != null) ?
        (string)lueDonVi.EditValue : null;

      try
      {
        DataSet dtsDsBenhNhan =
          GlobalCache.DieuTriService.DsBenhNhan(donVi, txtMaThe.Text, txtHoTen.Text,
            true, false, tuNgay, denNgay);
        _dtbDsBenhNhan = dtsDsBenhNhan.Tables[0];
      }
      catch (Exception ex)
      {
        Logger.LogCritical("FrmDsDieuTriArv::bwDataLoading_DoWork", ex, "General", null);
      }
    }

    private void bwDataLoading_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
      Cursor = Cursors.Default;
      MainFormUtils.StopProgress();

      ToggleEditControls(true);

      if (_dtbDsBenhNhan != null)
      {
        gcDsBenhNhan.DataSource = _dtbDsBenhNhan;
        MainFormUtils.SetLoadedStatus();
      }
      else
      {
        MainFormUtils.SetLoadedFailedStatus();
        UIUtils.ShowCannotLoadDataMessage();
      }
    }

    #endregion DataLoading BackgroundWorker

    private void edit_Enter(object sender, EventArgs e)
    {
      UIUtils.SelectAll((BaseEdit)sender);
    }

    #endregion

    #region Helper Methods

    private void BindData()
    {
      _donViList = GlobalCache.DonViService.GetAll();
      lueDonVi.Properties.DataSource = _donViList;
    }

    private void LoadData()
    {
      Cursor = Cursors.WaitCursor;
      MainFormUtils.StartProgress();
      MainFormUtils.SetLoadingStatus();
      ToggleEditControls(false);
      bwDataLoading.RunWorkerAsync();
    }

    private void ToggleEditControls(bool enabled)
    {
      btnSearch.Enabled = enabled;
      btnView.Enabled = enabled;
      btnEdit.Enabled = enabled;
      btnReject.Enabled = enabled;
      btnPrint.Enabled = enabled;
    }

    #endregion

  }
}